<template>
    <div>
        <!--        党建资料管理-->
        <!--        头查询 -->
        <el-row :gutter="4">
            <el-col :span="4">
                <el-input v-model.trim="keyword.teacherName" prefix-icon="fa fa-search" placeholder="学生姓名"
                          @keyup.enter.native="initPolicyDocment"></el-input>
            </el-col>

            <el-col :span="2">
                <el-button type="primary" icon="fa fa-search" @click="initListInfo"
                           v-permission="'policy:document:list'"> 搜 索
                </el-button>
            </el-col>
            <el-col :span="2" style="margin-left: 20px">
                <el-button type="primary" icon="fa fa-plus" @click="handleAdd"
                           v-permission="'policy:document:add'"> 添 加 信 息
                </el-button>
            </el-col>
            <el-col :span="2" style="margin-left: 20px">
                <el-button type="danger" icon="fa fa-trash" @click="handleMultipleDelete"
                           v-permission="'policy:document:delete'"> 删
                    除 多 个
                </el-button>
            </el-col>
            <el-col :span="1" style="margin-left: 20px;">
                <el-button type="warning" icon="fa fa-refresh"
                           @click="initStudentAwards">
                </el-button>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="24">
                <el-table width="100%" border :data="page.row" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="50" align="center"></el-table-column>
                    <el-table-column prop="studentsBase.stuName" label="姓名" align="center" fixed="left">
                    </el-table-column>
                    <el-table-column prop="studentsBase.stuGender" label="性别" align="center"></el-table-column>
                    <el-table-column prop="studentsBase.stuSchool" label="系" align="center"></el-table-column>
                    <el-table-column prop="studentsBase.stuMajor" label="专业" align="center"></el-table-column>
                    <el-table-column prop="studentsBase.stuClass" label="班级" align="center"></el-table-column>
                    <el-table-column prop="matchDetail" label="比赛名称" align="center"></el-table-column>
                    <el-table-column prop="matchAwardsLevel" label="比赛级别" align="center"></el-table-column>
                    <el-table-column prop="matchRank" label="获奖排名" align="center"></el-table-column>
                    <el-table-column prop="matchTeacher1" label="指导教师1" align="center"></el-table-column>
                    <el-table-column prop="matchTeacher2" label="指导教师2" align="center"></el-table-column>
                    <el-table-column prop="matchTeacher3" label="指导教师3" align="center"></el-table-column>
                    <el-table-column prop="matchTeacher4" label="指导教师4" align="center"></el-table-column>
                    <el-table-column prop="matchTeacher5" label="指导教师5" align="center"></el-table-column>
                    <el-table-column prop="matchFile" label="扫描件" align="center">
                        <template slot-scope="scope">
                            <el-link v-if="scope.row.matchFile" type="primary"
                                     :href="`${baseURL}${fileViewPath}${scope.row.matchFile}`"
                                     target="_blank">
                                <i class="fa fa-search" aria-hidden="true"></i>
                                {{ scope.row.matchFile | filterFileName }}
                            </el-link>
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="200">
                        <template slot-scope="scope">
                            <el-button @click="handleEdit(scope.row)" icon="fa fa-pencil"
                                       v-permission="'policy:document:update'"> 修 改
                            </el-button>
                            <el-button type="danger" @click="handleDelete(scope.row)" icon="fa fa-times-rectangle"
                                       v-permission="'policy:document:delete'"> 删 除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>

        <!--        分页 -->
        <el-row>
            <el-col :span="22">
                <el-pagination v-if="page.total > 10"
                               @size-change="handleSizeChange"
                               @current-change="handleCurrentChange"
                               :current-page="pagination.current"
                               :page-sizes="[9 ,10, 20, 30, 50]"
                               :page-size="pagination.size"
                               layout="total, sizes, prev, pager, next"
                               :total="page.total">
                </el-pagination>
            </el-col>
        </el-row>

        <el-dialog
                v-dialog-drag
                :title="isAdd ? '新 增 数 据' : '修 改 数 据'"
                :visible.sync="centerDialogVisible"
                width="40%"
                center
                @close="resetField"
        >

            <el-form ref="form" :rules="rules" :model="data" label-width="80px">
                <el-form-item label="学生姓名" label-width="auto">
                    <el-autocomplete
                            class="inline-input"
                            prefix-icon="fa fa-search"
                            v-model="data.studentsBase.stuName"
                            :fetch-suggestions="querySearch"
                            placeholder="学生姓名"
                            :trigger-on-focus="false"
                            clearable
                            @select="oneSelect"
                    ></el-autocomplete>
                </el-form-item>

                <el-form-item label="获奖年度" label-width="70px" prop="matchYear">
                    <el-date-picker
                            v-model="data.matchYear"
                            type="date"
                            format="yyyy"
                            value-format="yyyy"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="比赛时间" label-width="70px" prop="matchDate">
                    <el-date-picker
                            v-model="data.matchDate"
                            type="date"
                            format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="比赛地点" label-width="70px">
                    <el-input v-model="data.matchAddress"></el-input>
                </el-form-item>
                <el-form-item label="比赛名称" label-width="70px">
                    <el-input v-model="data.matchDetail"></el-input>
                </el-form-item>

                <el-form-item label="获奖级别:" prop="matchAwardsLevel">
                    <el-select v-model="data.matchAwardsLevel" placeholder="选择级别" >
                        <el-option
                                v-for="item in matchAwardsLevelList"
                                :key="item"
                                :label="item"
                                :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="获奖排名" label-width="70px">
                    <el-input v-model="data.matchRank"></el-input>
                </el-form-item>
                <el-form-item label="指导教师1" label-width="70px">
                    <el-input v-model="data.matchTeacher1"></el-input>
                </el-form-item>
                <el-form-item label="指导教师2" label-width="70px">
                    <el-input v-model="data.matchTeacher2"></el-input>
                </el-form-item>
                <el-form-item label="指导教师3" label-width="70px">
                    <el-input v-model="data.matchTeacher3"></el-input>
                </el-form-item>
                <el-form-item label="指导教师4" label-width="70px">
                    <el-input v-model="data.matchTeacher4"></el-input>
                </el-form-item>

                <el-form-item label="相关附件:" label-width="80px">
                    <el-upload
                            ref="upload"
                            drag
                            :headers="fileHeaders"
                            :action="baseURL.concat('file/upload')"
                            name="file"
                            :data="fileDir"
                            :on-success="handleFileUploadSuccess"
                            :on-error="handleFileUploadError"
                            :show-file-list=false
                    >
                        <i class="el-icon-upload"></i>
                        <div class="el-upload__text"> 拖 拽 或 <em>点 击 上 传 </em></div>
                        <div class="el-upload__tip" slot="tip">建议上传之后浏览 默认不解析 utf-8 以外字符</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="检查上传文件:" label-width="120px">
                    <el-link v-if="data.matchFile" :href="`${baseURL}${fileViewPath}${data.matchFile}`"
                             target="_blank">
                        {{ data.matchFile | filterFileName }} <i
                            class="el-icon-view el-icon--right"></i>
                    </el-link>
                    <el-tag v-else type="success"> 无 上 传 文 件</el-tag>
                </el-form-item>

            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="centerDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addOrUpdateStudentAwards">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>

    import {
        addStudentAwards,
        deleteMultipleStudentAwards,
        deleteStudentAwards,
        page,
        removeFile,
        updateStudentAwards
    } from "../../../api/modules/student/award";

    import {authKey, baseURL, fileViewPath} from "@/config/constant";
    import {mapState} from "vuex";
    import {form, recommendStudent} from "@/api/modules/dictionary";

    export default {
        name: 'StudentAward',

        data() {
            return {
                baseURL, fileViewPath,
                data: {
                    "id":"",
                    "stuId":"",
                    "matchYear":"",
                    "matchDate":"",
                    "matchAddress":"",
                    "matchDetail":"",
                    "matchAwardsLevel":"",
                    "matchRank":"",
                    "matchTeacher1":"",
                    "matchTeacher2":"",
                    "matchTeacher3":"",
                    "matchTeacher4":"",
                    "matchTeacher5":"",
                    "matchFile":"",
                    "status":"",
                    "createBy":"",
                    "createTime":"",
                    "updateBy":"",
                    "updateTime":"",
                    "delFlag":"",

                    "studentsBase":{
                        "stuNum":"",
                        "stuName":"",
                        "stuGender":"",
                        "stuSchool":"",
                        "stuMajor":"",
                        "stuClass":"",
                    }
                },
                matchAwardsLevelList:[
                    "国家级","省级"
                ],
                keyword: {},
                pagination: {
                    current: 1,
                    size: 9
                },
                // 推荐信息
                recommend: {
                    student: [],
                    form: {}
                },
                page: {
                    row: [],
                    total: 0
                },
                isAdd: true,
                multipleSelection: [],
                centerDialogVisible: false
            };
        },

        methods: {
            initRecommend() {
                recommendStudent().then(response => {
                    this.recommend.student = response;
                });
                form().then(response => {
                    this.recommend.form = response;
                });
            },
            // 查询
            querySearch(queryString, cb) {
                cb(this.recommend.student.filter(item => item.value.indexOf(queryString) !== -1))
            },

            oneSelect(item){
                this.data.stuId=item.id
            },
            initStudentAwards() {
                page(this.pagination, this.keyword).then(response => {
                    this.page.row = response.row;
                    this.page.total = response.total;
                })
            },
            // 添加单个
            addOrUpdateStudentAwards() {
                this.$refs.form.validate(valid => {
                    if (valid) {
                        // 是新增就进行添加
                        if (this.isAdd) {
                            addStudentAwards(this.data).then(() => {
                                // TODO 添加信息 不需要 init
                                this.centerDialogVisible = false;
                                this.initStudentAwards();
                            })
                        } else {
                            updateStudentAwards(this.data).then(() => {
                                // TODO 修改信息 不需要 init
                                this.centerDialogVisible = false;
                                this.initStudentAwards();
                            })
                        }
                    }
                })
            },
            // 点击选择
            handleSelect(value) {
                this.data.id = value.id;
                console.log(this.data);
            },
            // 当前页大小改变
            handleSizeChange(size) {
                this.pagination.size = size;
                this.initStudentAwards();
            },
            // 当前页改变
            handleCurrentChange(current) {
                this.pagination.current = current;
                this.initStudentAwards();
            },
            // 修改单个
            handleEdit(row) {
                // 不是添加
                this.isAdd = false;
                Object.assign(this.data, row);
                this.centerDialogVisible = true;
            },
            handleAdd() {
                // 是添加
                this.isAdd = true;
                Object.assign(this.data, {});
                this.centerDialogVisible = true;
            },
            // 删除单个
            handleDelete(row) {
                deleteStudentAwards(row.id).then(() => {
                    // 刷新页面
                    this.initStudentAwards();
                });
            },
            // 多选框
            handleSelectionChange(row) {
                // 获取 ids
                this.multipleSelection = row.map(item => item.id);
            },
            // 删除多个
            handleMultipleDelete() {
                // 大于 0 才删除
                if (this.multipleSelection.length > 0) {
                    deleteMultipleStudentAwards(this.multipleSelection).then(() => {
                        // 刷新页面
                        this.initStudentAwards();
                    });
                }
            },
            async handleFileUploadSuccess(response) {
                await removeFile(this.data.matchFile);
                if (response.code <= 30000 && response.code >= 20000) {
                    this.data.matchFile = response.data;
                    this.$message.success({message: response.msg})
                } else {
                    this.$message.error({message: response.msg})
                }
            },
            handleFileUploadError() {
                this.$message.error({message: "上传失败"})
            },
            resetField() {
                // 清空表单验证
                this.$refs.form.resetFields();
            }
        },

        computed: {
            ...mapState("user", ["userToken"]),
            fileHeaders() {
                // 文件上传携带头信息
                let obj = {};
                obj[authKey] = this.userToken;
                return obj
            },
            fileDir() {
                // 上传的文件位置
                return {dir: "awards"}
            },
            refresh() {
                this.initTeacherBase();
                this.initRecommend();
            }
        },

        filters: {
            filterFileName(value) {
                // 斜杠位置
                let slashIndex = value.lastIndexOf("/");
                // 文件随机数位置
                let randomIndex = value.indexOf("-");
                return value.slice(slashIndex + 1, randomIndex) + value.slice(value.indexOf('.'));
            }
        },
        mounted() {
            this.initStudentAwards();
            this.initRecommend()
        },
        watch: {
            keyword: {
                deep: true,
                handler() {
                    this.pagination.current = 1
                }
            }
        }
    }

</script>

<style scoped>

</style>